<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" :placeholder="$t('table.title')" v-model="listQuery.title">
      </el-input>
      <el-button class="filter-item" type="primary"  icon="el-icon-search" @click="handleFilter">{{$t('table.search')}}</el-button>
      <el-button class="filter-item" type="danger"  icon="el-icon-circle-plus-outline" @click="handleCreate">{{'新建'}}</el-button>
    </div>

    <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row
      style="width: 100%">
      <el-table-column align="center" :label="tableTitle.description">
        <template slot-scope="scope">
          <span>{{scope.row.description}}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column align="center" :label="tableTitle.belong">
        <template slot-scope="scope">
          <span>{{new Date(scope.row.createdAt) | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="操作"  class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status!='deleted'" size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="100px" style='width: 400px; margin-left:100px;'>
        <el-form-item :label="'角色名'" prop="description">
          <el-input v-model="temp.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{$t('table.cancel')}}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{$t('table.confirm')}}</el-button>
        <el-button v-else type="primary" @click="updateData">{{$t('table.confirm')}}</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import * as Api from '@/api/commodity/tag'
import { tabListMixin } from '@/base/mixins/tableMix'

export default {
  name: 'complexTable',
  mixins: [tabListMixin],
  data() {
    return {
      tableTitle: {
        role: '角色',
        description: '角色名',
        deleteFlag: '是否可用'
      },
      tableKey: 0,
      listQuery: {
        page: 1,
        limit: 10,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      temp: {
        name: undefined,
        deleteFlag: 0
      },
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }]
      }
    }
  },
  filters: {
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      Api.tagList({
        pageSize: this.listQuery.limit,
        isPage: 1,
        pageIndex: this.listQuery.page
      }).then(response => {
        this.list = response.data
        this.total = response.totalCount
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        name: undefined,
        deleteFlag: 0
      }
    },
    resetUpdate() {
      this.$confirm('是否重置密码?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '重置成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消重置'
        })
      })
    },
    createDataPost(data) {
      const reData = new Promise((resolve, reject) => {
        Api.tagCreate({
          name: data.name
        }).then(response => {
          resolve(response)
          // this.list = response.data
          // this.total = response.totalCount
          // this.listLoading = false
        })
      })
      return reData
    },
    updateDataPost(data) {
      const reData = new Promise((resolve, reject) => {
        Api.tagUpdate({
          name: data.name,
          id: data.id
        }).then(response => {
          resolve(response)
          // this.list = response.data
          // this.total = response.totalCount
          // this.listLoading = false
        })
      })
      return reData
    },
    deleteDataPost(data) {
      const reData = new Promise((resolve, reject) => {
        Api.editTagDeleteFlagById({
          id: data.id
        }).then(response => {
          resolve(response)
          // this.list = response.data
          // this.total = response.totalCount
          // this.listLoading = false
        })
      })
      return reData
    },
    searchDataPost(data) {
      this.listLoading = true
      const reData = new Promise((resolve, reject) => {
        Api.tagQuery({
          name: data
        }).then(response => {
          resolve(response)
          this.list = response.data
          this.total = response.totalCount
          this.listLoading = false
        })
      })
      return reData
    }
  }
}
</script>
